package com.doubledragon.tiny.aop.daynamicProxy;

import org.aopalliance.intercept.MethodInterceptor;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by dragon
 */
public class JdkDynamicProxy implements InvocationHandler, ProxyFactory {

    private AdvisedSupport advisedSupport;

    public JdkDynamicProxy(AdvisedSupport advisedSupport) {
        this.advisedSupport = advisedSupport;
    }

    @Override
    public Object getProxy() {
        Class<?> targetClass = advisedSupport.getTargetSource().getTargeObject().getClass();
        return Proxy.newProxyInstance(
                targetClass.getClassLoader(), targetClass.getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //调用拦截器的方法
        MethodInterceptor methodInterceptor = advisedSupport.getMethodInterceptor();
        return methodInterceptor.invoke(new ReflectiveMethodInvocation(advisedSupport.getTargetSource().getTargeObject(), method, args));
    }


}
